﻿using Gx.Bll.Manage;
using Gx.ColdWarehouseErp.Controllers;
using Gx.Common;
using Gx.Model.Entity;
using Gx.Model.Enums;
using Gx.Model.ViewModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Gx.ZhiChengXianWei.Controllers
{
    /// <summary>
    /// 商品单据控制器
    /// </summary>
    public class GoodsOrderController : BaseController
    {
        protected LKWarehouseEntryBll lKWarehouseEntryBll = new LKWarehouseEntryBll();
        protected LKWarehouseOutBll lKWarehouseOutBll = new LKWarehouseOutBll();
        protected LKWarehouseAdjustBll lKWarehouseAdjustBll = new LKWarehouseAdjustBll();
        protected LKTransferBll lKTransferBll = new LKTransferBll();
        protected LKBillTemplateBll templateBll = new LKBillTemplateBll();
        protected LKZeroWarehouseContractBll zeroWarehouseContractBll = new LKZeroWarehouseContractBll();
        #region 商品入库单
        public ActionResult LKWarehouseEntryList()
        {
            return View();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="customerInput"></param>
        /// <returns></returns>
        public JsonResult GetLKWarehouseEntryPageList(LKWarehouseEntryInput input)
        {
            if (input == null) return JsonList(0, new LKWarehouseEntry[] { });
            input.RentalWarehouseType = (int)RentalWarehouseTypeEnum.Warehouse;
            var result = lKWarehouseEntryBll.GetLKWarehouseEntryPageList(input);
            return JsonList(result.Total, result.List.ToArray());
        }


        /// <summary>
        /// 获取商品入库单
        /// </summary>
        /// <returns></returns>
        public JsonResult GetLKWarehouseEntry()
        {
            var result = lKWarehouseEntryBll.GetLKWarehouseEntrys();
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 新增商品入库单
        /// </summary>
        /// <param name="lKWarehouseEntry"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddLKWarehouseEntry(LKWarehouseEntryDto lKWarehouseEntry)
        {
            var result = lKWarehouseEntryBll.AddLKWarehouseEntry(lKWarehouseEntry);
            //string message = result ? "新增成功" : "新增失败";
            return Json(result.Success, result.Msg);
        }

        /// <summary>
        /// 编辑商品入库单
        /// </summary>
        /// <param name="lKWarehouseEntry"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditLKWarehouseEntry(LKWarehouseEntryDto lKWarehouseEntry)
        {
            var result = lKWarehouseEntryBll.EditLKWarehouseEntry(lKWarehouseEntry);
            string message = result ? "编辑成功" : "编辑失败";
            return Json(result, message);
        }

        ///// <summary>
        ///// 确认入库
        ///// </summary>
        ///// <param name="entryConfirm"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public JsonResult ConfirmLKWarehouseEntry(EntryConfirmDto entryConfirm)
        //{
        //    if (entryConfirm == null) return Json(false, "参数错误");
        //    var result = lKWarehouseEntryBll.ConfirmLKWarehouseEntry(entryConfirm.Id, entryConfirm.ContractNo, entryConfirm.ContractType);
        //    string message = result ? "确认成功" : "确认失败";
        //    return Json(result, message);
        //}

        /// <summary>
        /// 删除商品入库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DeleteLKWarehouseEntry(int id)
        {
            var result = lKWarehouseEntryBll.DeleteLKWarehouseEntry(id);
            return Json(result.Success, result.Msg);
            //string message = result ? "删除成功" : "删除失败";
            //return Json(result, message);
        }

        /// <summary>
        /// 添加包仓入库单视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AddLKWarehouseEntry()
        {
            //var temp = templateBll.GetLKBillTemplateByBillType(1);
            var bill = new LKWarehouseEntry();//JsonConvert.DeserializeObject<LKWarehouseEntry>(temp?.TemplateContent);
            if (bill != null)
            {
                bill.CreateUser = LoginHelper.UserName();
                bill.CreateUserId = LoginHelper.GetUserId();
                bill.CreateTime = DateTime.Now;
                bill.ModifyTime = DateTime.Now;
                bill.ModifyUserId = LoginHelper.GetUserId();
                bill.ModifyUser = LoginHelper.UserName();
            }
            return View(bill);
        }

        /// <summary>
        /// 添加商品入库单商品明细分部视图
        /// </summary>
        /// <param name="domId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult _AddLKWarehouseEntryDetail(int domId)
        {
            var code = lKWarehouseEntryBll.AddLKWarehouseEntryDetailGoodsCode();
            return PartialView(new EntryDetailView { DomId = domId, Code = code });
        }

        [HttpPost]
        public ActionResult _AddLKWarehouseEntryCost(int domId)
        {
            return PartialView(domId);
        }

        /// <summary>
        /// 编辑商品入库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult EditLKWarehouseEntry(int id)
        {
            var result = lKWarehouseEntryBll.GetLKWarehouseEntryById(id);
            return View(result);
        }

        public JsonResult GetKWarehouseEntryDetails(int entryId) 
        {
            var result = lKWarehouseEntryBll.GetLKWarehouseEntryDetails(entryId);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取客户下的商品入库单明细列表
        /// </summary>
        /// <param name="customerId"></param>
        /// <returns></returns>
        public JsonResult GetLKWarehouseEntryDetailsByCustomerId(GetGoodOutDetailInput input) 
        {
            if(input==null) return JsonList(0, new LKWarehouseEntryDetail[] { });
            var result = lKWarehouseEntryBll.GetLKWarehouseEntryDetailsByCustomerId(input.CustomerId);
            return JsonList(result.Count(), result.ToArray());
        }

        //入库单打印
        [HttpGet]
        public ActionResult LKWarehouseEntryPrint(int id)
        {
            var result = lKWarehouseEntryBll.GetLKWarehouseEntryById(id);
            return View(result);
        }

        /// <summary>
        /// 复核入库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ReviewLKWarehouseEntry(int id)
        {
            var result = lKWarehouseEntryBll.ReviewLKWarehouseEntry(id);
            string message = result ? "复核成功" : "复核失败";
            return Json(result, message);
        }

        #endregion

        #region 零仓入库单
        /// <summary>
        /// 零仓入库单列表
        /// </summary>
        /// <returns></returns>
        public ActionResult LKZeroWarehouseEntryList()
        {
            return View();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="customerInput"></param>
        /// <returns></returns>
        public JsonResult GetLKZeroWarehouseEntryPageList(LKWarehouseEntryInput input)
        {
            if (input == null) return JsonList(0, new LKWarehouseEntry[] { });
            input.RentalWarehouseType = (int)RentalWarehouseTypeEnum.ZeroWarehouse;
            var result = lKWarehouseEntryBll.GetLKWarehouseEntryPageList(input);
            return JsonList(result.Total, result.List.ToArray());
        }

        /// <summary>
        /// 零仓入库单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AddLKZeroWarehouseEntry()
        {
            var bill = new LKWarehouseEntry();
            if (bill != null)
            {
                bill.CreateUser = LoginHelper.UserName();
                bill.CreateUserId = LoginHelper.GetUserId();
                bill.CreateTime = DateTime.Now;
                bill.ModifyTime = DateTime.Now;
                bill.ModifyUserId = LoginHelper.GetUserId();
                bill.ModifyUser = LoginHelper.UserName();
            }
            return View(bill);
        }

        /// <summary>
        /// 编辑零仓入库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult EditLKZeroWarehouseEntry(int id)
        {
            var result = lKWarehouseEntryBll.GetLKWarehouseEntryById(id);
            return View(result);
        }

        /// <summary>
        /// 零仓入库单打印
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult LKZeroWarehouseEntryPrint(int id)
        {
            var result = lKWarehouseEntryBll.GetLKWarehouseEntryById(id);
            return View(result);
        }
        #endregion

        #region 商品出库单
        public ActionResult LKWarehouseOutList()
        {
            return View();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="customerInput"></param>
        /// <returns></returns>
        public JsonResult GetLKWarehouseOutPageList(LKWarehouseOutInput input)
        {
            var result = lKWarehouseOutBll.GetLKWarehouseOutPageList(input);
            return JsonList(result.Total, result.List.ToArray());
        }


        /// <summary>
        /// 获取商品出库单
        /// </summary>
        /// <returns></returns>
        public JsonResult GetLKWarehouseOut()
        {
            var result = lKWarehouseOutBll.GetLKWarehouseOuts();
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 新增商品出库单
        /// </summary>
        /// <param name="lKWarehouseOut"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddLKWarehouseOut(LKWarehouseOutDto lKWarehouseOut)
        {
            var result = lKWarehouseOutBll.AddLKWarehouseOut(lKWarehouseOut);
            //string message = result ? "新增成功" : "新增失败";
            return Json(result.Success, result.Msg);
        }

        /// <summary>
        /// 编辑商品出库单
        /// </summary>
        /// <param name="lKWarehouseOut"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditLKWarehouseOut(LKWarehouseOutDto lKWarehouseOut)
        {
            var result = lKWarehouseOutBll.EditLKWarehouseOut(lKWarehouseOut);
            string message = result ? "编辑成功" : "编辑失败";
            return Json(result, message);
        }

        /// <summary>
        /// 删除商品出库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DeleteLKWarehouseOut(int id)
        {
            var result = lKWarehouseOutBll.DeleteLKWarehouseOut(id);
            //string message = result ? "删除成功" : "删除失败";
            //return Json(result, message);
            return Json(result.Success, result.Msg);
        }

        /// <summary>
        /// 添加商品出库单视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AddLKWarehouseOut()
        {
            //var temp = templateBll.GetLKBillTemplateByBillType(1);
            var bill = new LKWarehouseOut();//JsonConvert.DeserializeObject<LKWarehouseOut>(temp?.TemplateContent);
            if (bill != null)
            {
                bill.CreateUser = LoginHelper.UserName();
                bill.CreateUserId = LoginHelper.GetUserId();
                bill.CreateTime = DateTime.Now;
                bill.ModifyTime = DateTime.Now;
                bill.ModifyUserId = LoginHelper.GetUserId();
                bill.ModifyUser = LoginHelper.UserName();
            }
            return View(bill);
        }

        /// <summary>
        /// 添加商品出库单分部视图
        /// </summary>
        /// <param name="domId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult _AddLKWarehouseOutDetail(int domId)
        {
            return PartialView(domId);
        }

        /// <summary>
        /// 编辑商品出库单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult EditLKWarehouseOut(int id)
        {
            var result = lKWarehouseOutBll.GetLKWarehouseOutById(id);
            return View(result);
        }

        /// <summary>
        /// 获取商品出库单明细
        /// </summary>
        /// <param name="outId"></param>
        /// <returns></returns>
        public JsonResult GetKWarehouseOutDetails(int outId)
        {
            var result = lKWarehouseOutBll.GetLKWarehouseOutDetails(outId);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        //出库单打印
        [HttpGet]
        public ActionResult LKWarehouseOutPrint(int id)
        {
            var result = lKWarehouseOutBll.GetLKWarehouseOutById(id);
            return View(result);
        }

        /// <summary>
        /// 复核出库单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ReviewLKWarehouseOut(int id)
        {
            var result = lKWarehouseOutBll.ReviewLKWarehouseOut(id);
            string message = result ? "复核成功" : "复核失败";
            return Json(result, message);
        }

        #endregion

        #region 商品寄存单
        public ActionResult LKWarehouseDepositList()
        {
            return View();
        }
        #endregion

        #region 商品调仓单
        public ActionResult LKWarehouseAdjustList()
        {
            return View();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="customerInput"></param>
        /// <returns></returns>
        public JsonResult GetLKWarehouseAdjustPageList(LKWarehouseAdjustInput input)
        {
            var result = lKWarehouseAdjustBll.GetLKWarehouseAdjustPageList(input);
            return JsonList(result.Total, result.List.ToArray());
        }


        /// <summary>
        /// 获取商品调仓单
        /// </summary>
        /// <returns></returns>
        public JsonResult GetLKWarehouseAdjust()
        {
            var result = lKWarehouseAdjustBll.GetLKWarehouseAdjusts();
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 新增商品调仓单
        /// </summary>
        /// <param name="lKWarehouseAdjust"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddLKWarehouseAdjust(LKWarehouseAdjustDto lKWarehouseAdjust)
        {
            var result = lKWarehouseAdjustBll.AddLKWarehouseAdjust(lKWarehouseAdjust);
            //string message = result ? "新增成功" : "新增失败";
            return Json(result.Success, result.Msg);
        }

        /// <summary>
        /// 编辑商品调仓单
        /// </summary>
        /// <param name="lKWarehouseAdjust"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditLKWarehouseAdjust(LKWarehouseAdjustDto lKWarehouseAdjust)
        {
            var result = lKWarehouseAdjustBll.EditLKWarehouseAdjust(lKWarehouseAdjust);
            string message = result ? "编辑成功" : "编辑失败";
            return Json(result, message);
        }

        /// <summary>
        /// 删除商品调仓单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DeleteLKWarehouseAdjust(int id)
        {
            var result = lKWarehouseAdjustBll.DeleteLKWarehouseAdjust(id);
            string message = result ? "删除成功" : "删除失败";
            return Json(result, message);
        }

        /// <summary>
        /// 添加商品调仓单视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AddLKWarehouseAdjust()
        {
            //var temp = templateBll.GetLKBillTemplateByBillType(1);
            var bill = new LKWarehouseAdjust();//JsonConvert.DeserializeObject<LKWarehouseAdjust>(temp?.TemplateContent);
            if (bill != null)
            {
                bill.CreateUser = LoginHelper.UserName();
                bill.CreateUserId = LoginHelper.GetUserId();
                bill.CreateTime = DateTime.Now;
                bill.ModifyTime = DateTime.Now;
                bill.ModifyUserId = LoginHelper.GetUserId();
                bill.ModifyUser = LoginHelper.UserName();
            }
            return View(bill);
        }

        /// <summary>
        /// 添加商品调仓单分部视图
        /// </summary>
        /// <param name="domId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult _AddLKWarehouseAdjustDetail(int domId)
        {
            return PartialView(domId);
        }

        /// <summary>
        /// 编辑商品调仓单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult EditLKWarehouseAdjust(int id)
        {
            var result = lKWarehouseAdjustBll.GetLKWarehouseAdjustById(id);
            return View(result);
        }
        #endregion

        #region 商品转卡单
        public ActionResult LKTransferList()
        {
            return View();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="customerInput"></param>
        /// <returns></returns>
        public JsonResult GetLKTransferPageList(LKTransferInput input)
        {
            var result = lKTransferBll.GetLKTransferPageList(input);
            return JsonList(result.Total, result.List.ToArray());
        }


        /// <summary>
        /// 获取商品转卡单
        /// </summary>
        /// <returns></returns>
        public JsonResult GetLKTransfer()
        {
            var result = lKTransferBll.GetLKTransfers();
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 新增商品转卡单
        /// </summary>
        /// <param name="lKTransfer"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddLKTransfer(LKTransferDto lKTransfer)
        {
            var result = lKTransferBll.AddLKTransfer(lKTransfer);
            //string message = result ? "新增成功" : "新增失败";
            return Json(result.Success, result.Msg);
        }

        /// <summary>
        /// 编辑商品转卡单
        /// </summary>
        /// <param name="lKTransfer"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditLKTransfer(LKTransferDto lKTransfer)
        {
            var result = lKTransferBll.EditLKTransfer(lKTransfer);
            string message = result ? "编辑成功" : "编辑失败";
            return Json(result, message);
        }

        /// <summary>
        /// 删除商品转卡单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DeleteLKTransfer(int id)
        {
            var result = lKTransferBll.DeleteLKTransfer(id);
            string message = result ? "删除成功" : "删除失败";
            return Json(result, message);
        }

        /// <summary>
        /// 添加商品转卡单视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AddLKTransfer()
        {
            //var temp = templateBll.GetLKBillTemplateByBillType(1);
            var bill = new LKTransfer(); //JsonConvert.DeserializeObject<LKTransfer>(temp?.TemplateContent);
            if (bill != null)
            {
                bill.CreateUser = LoginHelper.UserName();
                bill.CreateUserId = LoginHelper.GetUserId();
                bill.CreateTime = DateTime.Now;
                bill.ModifyTime = DateTime.Now;
                bill.ModifyUserId = LoginHelper.GetUserId();
                bill.ModifyUser = LoginHelper.UserName();
            }
            return View(bill);
        }

        /// <summary>
        /// 添加商品转卡单分部视图
        /// </summary>
        /// <param name="domId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult _AddLKTransferDetail(int domId)
        {
            return PartialView(domId);
        }

        [HttpPost]
        public ActionResult _AddLKTransferOutCost(int domId)
        {
            return PartialView(domId);
        }

        /// <summary>
        /// 编辑商品转卡单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult EditLKTransfer(int id)
        {
            var result = lKTransferBll.GetLKTransferById(id);
            return View(result);
        }
        #endregion
    }
}